package com.shopping.mall.core.module.address.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.auth.utils.UserUtils;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.address.entity.Address;
import com.shopping.mall.core.module.address.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "收货地址")
@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<Address>> getAddressPage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        Address address) {
        IPage<Address> addressPage=addressService.getAddressPage(current,size,address);
        return Result.OK(addressPage);
    }

    @ApiOperation("修改")
    @PutMapping
    public Result<Address> update(@Validated(Address.update.class) @RequestBody Address address) {
        addressService.updateById(address);
        return Result.OK();
    }

    @ApiOperation("新增")
    @PostMapping
    public Result<Object> add(@Validated(Address.insert.class) @RequestBody Address address) {
        address.setUserId(UserUtils.getLoginUserId());
        addressService.save(address);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<Address> getById(@RequestParam String id){
        Address address=addressService.getById(id);
        return Result.OK(address);
    }

    @ApiOperation("查询我的地址")
    @GetMapping
    public Result<List<Address>> getAll(Address address){
        address.setUserId(UserUtils.getLoginUserId());
        List<Address> addressList=addressService.getAll(address);
        return Result.OK(addressList);
    }

    @ApiOperation("通过id删除")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return addressService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return addressService.batchDelete(ids);
    }
}
